package com.acegear.horizon.domain.models.jpa;

import com.fasterxml.jackson.annotation.JsonIgnore;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.util.Arrays;
import java.util.Objects;
import java.util.UUID;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

/**
 * Created by wangsike on 2016/12/10.
 */
@Entity
@EntityListeners(AuditingEntityListener.class)
public class UserAuth {

    @Column(unique = true)
    private Long userId;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    private Long id;

    @JsonIgnore
    @Column
    private String accessToken;

    @Column
    private Boolean active;

    @Column
    private Boolean manageUser;

    @Column
    private String role;

    @CreatedDate
    @Column
    private Long createAt;

    @LastModifiedDate
    @Column
    private Long updateAt;

    public UserAuth() {

    }

    public UserAuth(Long userId) {
        this.userId = userId;
        init();
    }

    public Long getCreateAt() {
        return createAt;
    }

    public Long getUpdateAt() {
        return updateAt;
    }

    private void init() {
        this.active = true;
        this.manageUser = false;
        this.role = "";
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public Boolean getActive() {
        return active;
    }

    public void active() {
        this.active = true;
    }

    public void deactive() {
        this.active = false;
    }

    public UserAuth newToken() {
        setAccessToken(UUID.randomUUID().toString());
        return this;
    }

    public UserAuth clearToken() {
        setAccessToken(null);
        return this;
    }

    public boolean checkToken(String accessToken) {
        return Objects.equals(this.getAccessToken(), accessToken);
    }

    public Boolean getManageUser() {
        return manageUser;
    }

    public void manage() {
        manageUser = true;
    }

    public String getRole() {
        return role;
    }

    public Boolean checkRole(String role) {
        return this.role.contains(role);
    }

    public void addRole(String role) {
        if (this.role.isEmpty()) {
            this.role = role;
        } else {
            this.role = this.role + ',' + role;
        }
    }

    public void removeRole(String role) {
        this.role =
                Arrays.stream(this.role.split(","))
                        .filter(r -> !r.equals(role))
                        .reduce((s, s1) -> s + ',' + s1).get();
    }

    public void unmanage() {
        manageUser = false;
    }
}
